package com.ysstech.etfmanage.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.etfmanage.entity.Param;
import com.ysstech.etfmanage.entity.ParamVo;
import com.ysstech.etfmanage.enums.EtfManageEnum;
import com.ysstech.etfmanage.feign.FeignFinanceService;
import com.ysstech.etfmanage.service.ParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@Api(tags = "ETF参数api")
@RequestMapping("/param")
public class ParamController extends BaseController {

    @Autowired
    private ParamService paramService;
    @Autowired
    private FeignFinanceService feignFinanceService;

    @YssPermissions(value = {"etfmanage:param"})
    @ApiOperation(value = "跳转ETF参数管理页面")
    @YssLog(module = "ETF参数管理", remark = "跳转ETF参数管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "etfmanage/param";
    }


    @YssPermissions(value = {"etfmanage:param"})
    @ApiOperation(value = "初始化ETF参数管理页面下拉选项的值")
    @YssLog(module = "ETF参数管理", remark = "初始化ETF参数管理页面下拉选项的值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //ETF的套账
        List<TreeNode> acctounts = feignFinanceService.queryAccountTreeByType(redisCacheManager.getLoginUserInfo().getOrgId(), 5, null);
        map.put("acctounts", acctounts);
        //etf模型 股票，债券，期货
        List<Dicvalue> list = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_MODE_CODE.getCode());
        map.put("etfMode", list);
        //etf市场
        list = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_MARKET_CODE.getCode());
        map.put("etfMarket", list);
        // etf类型  单市场，跨市场，集中申购
        list = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_TYPE_CODE.getCode());
        map.put("etfType", list);
        // ETF台账更新方式
        list = dicValueService.getCacheDicValueList(EtfManageEnum.ETF_RENEWTYPE_CODE.getCode());
        map.put("etfRenewType", list);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }


    /**
     * 查询参数
     *
     * @return
     */
    @YssPermissions(value = {"etfmanage:param"})
    @ApiOperation(value = "查询ETF参数信息")
    @YssLog(module = "ETF参数管理", remark = "查询ETF参数信息")
    @RequestMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(Param param) throws Exception {
        if (null == param || StringUtils.isBlank(param.getAccountId())) {
            throw new BusinessException("获取ETF参数信息失败");
        }
        List<String> list = JSONArray.parseArray(param.getAccountId(), String.class);
        param.setAccountId(list.get(0));
        List<Object> paramList = paramService.getParamList(param, ParamVo.class);
        ParamVo paramVo = null;
        for (Object obj : paramList) {
            paramVo = (ParamVo) obj;
            break;
        }
        return ResultGenerator.getSuccessResult("查询套账ETF参数信息成功！", paramVo);
    }


    /**
     * 新增参数
     *
     * @return
     */
    @YssPermissions(value = {"etfmanage:param"})
    @ApiOperation(value = "新增ETF参数信息")
    @YssLog(module = "ETF参数管理", remark = "新增ETF参数信息")
    @RequestMapping(value = "/add")
    @ResponseBody
    public RestResult add(String paramarry, ParamVo paramVo) throws Exception {
        if (StringUtils.isBlank(paramarry)) {
            throw new BusinessException("获取ETF参数信息失败");
        }
        List<Param> list = JSONArray.parseArray(paramarry, Param.class);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取ETF参数信息失败");
        }
        List<String> accountIds = JSONArray.parseArray(paramVo.getAccountId(), String.class);
        if (CollectionUtils.isEmpty(accountIds)) {
            throw new BusinessException("获取ETF基金信息失败");
        }
        for (String accountId : accountIds) {
            for (Param param : list) {
                param.setEtfMarket(paramVo.getEtfMarket());
                param.setEtfMode(paramVo.getEtfMode());
                param.setEtfType(paramVo.getEtfType());
                param.setAccountId(accountId);
                param.setId(DateUtil.generateUUID());
                param.setAddTime(LocalDateTime.now());
            }
        }
        QueryWrapper<Param> wrapper = new QueryWrapper<>();
        wrapper.in("ACCOUNT_ID", accountIds);
        wrapper.eq("ETF_TYPE", paramVo.getEtfType());
        wrapper.eq("ETF_MODE", paramVo.getEtfMode());
        wrapper.eq("ETF_MARKET", paramVo.getEtfMarket());
        paramService.remove(wrapper);
        paramService.saveBatch(list);
        return ResultGenerator.getSuccessResult("新增ETF参数信息成功！");
    }

    /**
     * 刪除参数
     *
     * @return
     */
    @YssPermissions(value = {"etfmanage:param"})
    @ApiOperation(value = "刪除ETF参数信息")
    @YssLog(module = "ETF参数管理", remark = "刪除ETF参数信息")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult del(Param param) throws Exception {
        List<String> list = JSONArray.parseArray(param.getAccountId(), String.class);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取ETF参数信息失败");
        }
        param.setAccountId(list.get(0));
        QueryWrapper<Param> wrapper = new QueryWrapper<>();
        wrapper.eq("ACCOUNT_ID", param.getAccountId());
        wrapper.eq("ETF_TYPE", param.getEtfType());
        wrapper.eq("ETF_MODE", param.getEtfMode());
        wrapper.eq("ETF_MARKET", param.getEtfMarket());
        paramService.remove(wrapper);
        return ResultGenerator.getSuccessResult("刪除ETF参数信息成功！");
    }
}
